from openpyxl import load_workbook
import xlrd
import math


def read_excel(filename, sheetname):
    book = xlrd.open_workbook(filename)
    sheet = book.sheet_by_name(sheetname)
    puck = []
    rows = sheet.nrows  # 获取行数
    for r in range(rows):  # 读取每一行的数据
        r_values = sheet.row_values(r)
        puck.append(r_values)
    return puck


P = read_excel('Pucks(1).xlsx', 'Sheet1')
G = read_excel('InputData2.xlsx', 'Gates')
G.pop(0)


# print(P)

def judge(num):
    if (int(num) % 5 == 0):
        return num
    else:
        return num + 1


class Puck():
    def __init__(self, no, arriveMoment, arriveFlight, arriveType, planeType, setoffMoment, setoffFlight, setoffType):
        self.no = int(no)
        self.arriveMoment = judge(int(arriveMoment))
        self.arriveFlight = arriveFlight
        self.allocation = 0
        if arriveType == 'D':
            self.arriveType = 0
        else:
            self.arriveType = 1
        if planeType == 'N':
            self.planeType = 0
        else:
            self.planeType = 1
        self.setoffMoment = judge(int(setoffMoment))
        self.setoffFlight = setoffFlight
        if setoffType == 'D':
            self.setoffType = 0
        else:
            self.setoffType = 1
        self.T = self.arriveType * 4 + self.setoffType * 2 + self.planeType


##登机口类
class Gate():
    def __init__(self, number, arriveType, setoffType, planeType):
        self.number = number
        self.arriveType = arriveType
        self.setoffType = setoffType
        self.planeType = planeType
        self.allocation = 0
        self.planeList = []
        if arriveType == 'D':
            self.arriveType = 0
        elif arriveType == 'I':
            self.arriveType = 1
        else:
            self.arriveType = 2
        if planeType == 'N':
            self.planeType = 0
        else:
            self.planeType = 1
        if setoffType == 'D':
            self.setoffType = 0
        elif setoffType == 'I':
            self.setoffType = 1
        else:
            self.setoffType = 2


def getP_G():
    # global Pucks
    Pucks = []
    for i in range(len(P)):
        Pucks.append(Puck(P[i][0], P[i][2], P[i][3], P[i][4], P[i][5], P[i][7], P[i][8], P[i][9]))
    # global Gates
    Gates = []
    for i in range(len(G)):
        Gates.append(Gate(G[i][0], G[i][3], G[i][4], G[i][5]))
    return [Pucks, Gates]


[Pucks, Gates] = getP_G()
interval = []
intervalDict = {}
for i in Pucks:
    interval.append(i.setoffMoment - i.arriveMoment)
    intervalDict[int(i.no)] = i.setoffMoment - i.arriveMoment

# for i in intervalDict:
#     print(i,intervalDict[i])

# dict2 = sorted(intervalDict.values())
pucksTuple = sorted(intervalDict.items(), key=lambda x: x[1])


# 类型匹配，且飞机未分配
def cal(i, j):
    global pucksTuple
    print(Pucks[i[0] - 1].no)
    if j.arriveType == Pucks[i[0] - 1].arriveType and j.setoffType == Pucks[i[0] - 1].setoffType and j.planeType == \
            Pucks[i[0] - 1].planeType and Pucks[i[0] - 1].allocation == 0:
        return 1

    if j.arriveType == 2 and j.setoffType == Pucks[i[0] - 1].setoffType and j.planeType == \
            Pucks[i[0] - 1].planeType and Pucks[i[0] - 1].allocation == 0:
        return 1
    if j.arriveType == Pucks[i[0] - 1].arriveType and j.setoffType == 2 and j.planeType == \
            Pucks[i[0] - 1].planeType and Pucks[i[0] - 1].allocation == 0:
        return 1
    if j.arriveType == 2 and j.setoffType == 2 and j.planeType == \
            Pucks[i[0] - 1].planeType and Pucks[i[0] - 1].allocation == 0:
        return 1
    return 0


def interJudge(i, j):
    global Pucks
    global Gates
    global pucksTuple
    # 此飞机已经被分配了
    if Pucks[pucksTuple[i][0] - 1].allocation == 1:
        return 0
    # 未分配
    else:
        f = 0
        for g in range(len(Gates)):
            # 欢迎第一架飞机
            print(len(Gates[g].planeList), Gates[g].number)
            if len(Gates[j].planeList) == 0 and Gates[g].number == Gates[j].number:
                Gates[j].planeList.append(Pucks[pucksTuple[i][0] - 1])
                Gates[j].allocation = 1
                Pucks[pucksTuple[i][0] - 1].allocation = 1
                print(Pucks[pucksTuple[i][0] - 1].no)
                return 1

            # 其他飞机
            flag = 0
            # 判断即将分配的飞机，是否和登机口登记的飞机有冲突
            if Gates[g].number != Gates[j].number:
                continue
            for gatePlane in Gates[g].planeList:
                # 待分配飞机在已登记的飞机时间段内
                print(Pucks[pucksTuple[i][0] - 1].arriveMoment,gatePlane.arriveMoment,Pucks[
                    pucksTuple[i][0] - 1].setoffMoment,gatePlane.setoffMoment)
                if Pucks[pucksTuple[i][0] - 1].arriveMoment >= gatePlane.arriveMoment and Pucks[
                    pucksTuple[i][0] - 1].setoffMoment <= gatePlane.setoffMoment+45:
                    flag = 1
                    print(Pucks[pucksTuple[i][0] - 1].no, gatePlane.no)
                    # break
                #                               外
                elif Pucks[pucksTuple[i][0] - 1].arriveMoment <= gatePlane.arriveMoment and Pucks[
                    pucksTuple[i][0] - 1].setoffMoment >= gatePlane.setoffMoment:
                    flag = 1
                    print(Pucks[pucksTuple[i][0] - 1].no, gatePlane.no)
                    # break
                #                             已分配飞机落地时间在 未分配内
                elif gatePlane.arriveMoment >= Pucks[pucksTuple[i][0] - 1].arriveMoment and gatePlane.arriveMoment <= \
                        Pucks[pucksTuple[i][0] - 1].setoffMoment:
                    flag = 1
                    print(Pucks[pucksTuple[i][0] - 1].no, gatePlane.no)
                    # break
                #                       已分配飞机起飞时间在 未分配内
                elif gatePlane.setoffMoment+ 45 >= Pucks[pucksTuple[i][0] - 1].arriveMoment and gatePlane.setoffMoment <= \
                        Pucks[pucksTuple[i][0] - 1].setoffMoment:
                    flag = 1
                    print(Pucks[pucksTuple[i][0] - 1].no, gatePlane.no)
                    # break
                # elif Pucks[pucksTuple[i][0] - 1].arriveMoment > gatePlane.setoffMoment + 45:
                #     print(Pucks[ pucksTuple[i][0] - 1].no, gatePlane.no)
                #     f+=1
                #     continue
                # elif Pucks[pucksTuple[i][0] - 1].setoffMoment + 45 > gatePlane.arriveMoment:
                #     print(Pucks[pucksTuple[i][0] - 1].no, gatePlane.no)
                #     f+=1
                #     continue
                else:
                    print(Pucks[pucksTuple[i][0] - 1].no, gatePlane.no)
                    print("accident")
                    continue
            if flag == 0 :
                print(Gates[j].number)
                Gates[j].planeList.append(Pucks[pucksTuple[i][0] - 1])
                Gates[j].allocation = 1
                Pucks[pucksTuple[i][0] - 1].allocation = 1
                # print(Pucks[pucksTuple[i][0] - 1].no, gatePlane.no)
                return 1

    return 0


for i in range(len(pucksTuple)):
    print(pucksTuple[i][0], pucksTuple[i][1])
    # 1.按间隔顺序排列飞机，并设置分配标志
    # 2.按类别最小则优先级最高的方式分配登机口，设置分类标志
    # 3.每架飞机都要判断，自己与其他飞机是否公用一个登机口，时间差是否大于45分钟，大于则共用一个，小于则换下一个空闲登机口
    for j in range(len(Gates)):
        # 一一对应
        # print(cal(i, j),interJudge(Pucks, Gates, i, j))
        f1 = cal(pucksTuple[i], Gates[j])
        if f1:
            f=interJudge(i, j)
            print(Gates[j].planeList)
            if f :
                break
            else:
                continue
            # break
        else:
            continue
        # f2 = interJudge(Pucks, Gates, i, j)
        # f3 = 0
        # if f1 and f2:
        #     j.allocation = 1
        #     Pucks[i[0] - 1].allocation = 1
        #     f3 = 1
        #     # j.planeList.append(Pucks[i[0] - 1])
        #     print(j.planeList)
        # else:
        #     continue
        # if f3:
        #     j.planeList.append(Pucks[i[0] - 1])
        # print(j.planeList)

pCount = 0
gCount = 0
for i in Pucks:
    # print(i.allocation)
    if i.allocation == 1:
        pCount += 1
for i in Gates:
    if i.allocation == 1:
        gCount += 1
        print(i.number)

    # print(i.planeList[].no)
print(pCount, gCount)
pls = []

for i in Gates:
    pl = []
    for j in range(len(i.planeList)):
        pl.append(i.planeList[j].no)
    pls.append(pl)
print(pls)

# for i in pucksTuple:
#     print(i[0],i[1])
